import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

/**
 * @author: gao
 * @create-date: 2025/4/22 9:29
 */

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode() {
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}

public class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) return true;
        if ((p != null && q == null) || (p == null && q != null)) return false;
        if (p.val != q.val) return false;

        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }

    public static TreeNode BuildTree(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);

        int i = 1;
        while (!queue.isEmpty() && i < nums.length) {
            TreeNode currentNode = queue.poll();

            if (i < nums.length && nums[i] != -1) {
                currentNode.left = new TreeNode(nums[i]);
                queue.offer(currentNode.left);
            }
            i++;

            if (i < nums.length && nums[i] != -1) {
                currentNode.right = new TreeNode(nums[i]);
                queue.offer(currentNode.right);
            }
            i++;
        }

        return root;
    }

    public static void main(String[] args) {
        int[] arr1 = {1, -1, 2};
        int[] arr2 = {1, 2};

        TreeNode p = BuildTree(arr1);
        TreeNode q = BuildTree(arr2);

        Solution solution = new Solution();
        System.out.println(solution.isSameTree(p, q));
    }
}